package be.myandroid.games;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Circle {

	protected static final float SIZE = 0.05f;
	private float color[] = {0.63671875f, 0.76953125f, 0.22265625f};
	
	private static final int SEGS = 16;
	private final int ptCount = SEGS + 2;
	
	protected static final int X = 0;
	protected static final int Y = 1;
	protected static final int Z = 2;
	
	protected float[] pos = new float[3];
	protected float size = SIZE;
	
    private float[] mPts;
	private FloatBuffer triangleVB;
	
    public Circle(float xPos, float yPos, float zPos){
		
    	this.pos[X] = xPos;
    	this.pos[Y] = yPos;
    	this.pos[Z] = zPos;
    	
		mPts = new float[ptCount * 3];

		mPts[X] = 0;
		mPts[Y] = 0;
		mPts[Z] = 0;
		
		for (int i = 1; i < ptCount; i++) {
			mPts[i*3 + X] = (float) (Math.cos((2*Math.PI*i)/SEGS));
			mPts[i*3 + Y] = (float) (Math.sin((2*Math.PI*i)/SEGS));
			mPts[i*3 + Z] = 0f;
		}

		ByteBuffer vbb = ByteBuffer.allocateDirect(
				// (# of coordinate values * 4 bytes per float)
				mPts.length * 4); 
		vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order
		triangleVB = vbb.asFloatBuffer();  // create a floating point buffer from the ByteBuffer
		triangleVB.put(mPts);    // add the coordinates to the FloatBuffer
		triangleVB.position(0);            // set the buffer to read the first coordinate
	}
    
    public void setColor(float red, float green, float blue){
    	this.color[0] = red;
    	this.color[1] = green;
    	this.color[2] = blue;
    }

	public void draw(GL10 gl) {
		// Save the current matrix.
		gl.glPushMatrix();
		
		gl.glTranslatef(-pos[X],pos[Y],pos[Z]);
		gl.glScalef(size, size, size);
		
		gl.glColor4f(color[0],color[1],color[2], 0.7f);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleVB);
		gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, ptCount);
		
		// Restore the last matrix.
		gl.glPopMatrix();
	}
}
